package com.srit.core.struts2.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 单文件上传Action基类
 * 
 * @author guan
 * 
 */
public class UploadAction extends BaseAction{
	static Logger log = LoggerFactory.getLogger(UploadAction.class);
	protected File upload;// The actual file
	protected String uploadContentType; // The content type of the file
	protected String uploadFileName; // The uploaded file name
	private String fileCaption;

	public void setUpload(File file) {
		this.upload = file;
	}

	public void setUploadContentType(String contentType) {
		this.uploadContentType = contentType;
	}

	public void setUploadFileName(String filename) {
		this.uploadFileName = filename;
	}

	public void setFileCaption(String fileCaption) {
		this.fileCaption = fileCaption;
	}

	public String getUploadFileName() {
		return this.uploadFileName;
	}

	public void copyFile(String srcFilePath, String destFilePath)
			throws IOException {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		File srcFile = new File(srcFilePath);
		File destFile = new File(destFilePath);
		if (!srcFile.exists()) {
			throw new IOException("src file is not found");
		}
		if (!destFile.exists()) {
			destFile.createNewFile();
		}
		try {
			bis = new BufferedInputStream(new FileInputStream(srcFile));
			bos = new BufferedOutputStream(new FileOutputStream(destFile));
			byte[] buf = new byte[1024];
			while (bis.read(buf) != -1) {
				bos.write(buf);
			}
			bos.flush();
		} catch (FileNotFoundException e) {
			log.info("file not found");
			e.printStackTrace();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bis != null) {
				bis.close();
			}
			if (bos != null) {
				bos.close();
			}
		}

	}
}
